package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppHelpService;
import com.vf.admin.client.service.AppSetupService;
import com.vf.admin.client.service.AppSuggestService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.controller.AfterGiftController;
import com.vf.admin.miaodong.entity.AppHelp;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppSuggest;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.utils.StringEx.isNull;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppSheZhiController.java
 * @Description 设置管理接口
 * @createTime 2021年07月22日 19:04:00
 */
@Controller
@RequestMapping("/appshezhi")
public class AppSheZhiController extends ClientBaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AppUserService appUserService;
    @Autowired
    AppSuggestService appSuggestService;
    @Autowired
    AppSetupService appSetupService;
    @Autowired
    AppHelpService appHelpService;
    @Autowired
    private AfterGiftController afterGiftController;
    @Autowired
    RedisUtil redisUtil;

    /*
     * @Description 用户礼物特效开关
     * @Param: [leixing] 1开启 2关闭
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/10/22
     */
    @ResponseBody
    @RequestMapping("/liwu")
    public Result liwu(Integer leixing) {
        try {
            if (leixing == null) {
                return this.errorJSON("操作类型不存在！");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            if (user.getGiftfunction().intValue() == leixing.intValue()) {
                return this.errorJSON("不可重复操作！");
            }
            if (leixing.intValue() == 1) {
                user.setGiftfunction(1);
            } else if (leixing.intValue() == 2) {
                user.setGiftfunction(2);
            } else {
                return this.errorJSON("操作类型不存在！");
            }
            appUserService.updateLiWu(user.getId(), leixing);
            return this.sucessJSON("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("用户礼物特效开关接口异常" + e);
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }


    /*
     * @Description 用户附近开关
     * @Param: [leixing] 1开启 2关闭
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/10/22
     */
    @ResponseBody
    @RequestMapping("/fujin")
    public Result fujin(Integer leixing) {
        try {
            if (leixing == null) {
                return this.errorJSON("操作类型不存在！");
            }

            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);

            if (user.getNearfunction().intValue() == leixing.intValue()) {
                return this.errorJSON("不可重复操作！");
            }

            if (leixing.intValue() == 1) {
                user.setNearfunction(1);
            } else if (leixing.intValue() == 2) {
                user.setNearfunction(2);
            } else {
                return this.errorJSON("操作类型不存在！");
            }
            appUserService.updateweizhi(user.getId(), leixing);
            return this.sucessJSON("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("用户附近开关开关接口异常" + e);
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }


    /**
     * @Description 意见反馈
     * @Param: [neirong] 反馈内容
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/10/23
     */
    @ResponseBody
    @RequestMapping("/yijian")
    public Result yijian(String neirong, MultipartFile[] image) {
        try {
            if (StringEx.isNull(neirong)) {
                return this.errorJSON("反馈内容不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            String images = "";
            //判断是否上传动态图片
            if (image != null && image.length > 0) {
                //上传：循环获取file数组中得文件
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < image.length; i++) {
                    MultipartFile files = image[i];
                    if (!files.isEmpty()) {
                        //判断图片大小
                        if (files.getSize() <= 1073741824) {
//                            Result result = afterGiftController.uploadLiveImg(files);
                            String ossUrl = appUserService.aliyunOssupload(files);
                            /*Map<String,Object> map = JSONObject.parseObject(JSON.toJSONString(result.getData()));
                            String ossUrl = map.get("name").toString();*/
                            if (isNull(ossUrl)) {
                                return this.errorJSON("图片上传失败");
                            }
                            //拼接
                            stringBuffer.append(ossUrl + ",");
                        } else {
                            return this.errorJSON("图片大小超出限制");
                        }
                    }
                }
                //保存图片拼接路径
                images = stringBuffer.toString();
            }
            //保存意见反馈
            AppSuggest appSuggest = new AppSuggest();
            appSuggest.setUserid(user.getId());//用户id
            appSuggest.setNick(user.getNick());//用户昵称
            appSuggest.setUserid(user.getId());//用户id
            appSuggest.setPhone(user.getLoginname());//用户手机号
            appSuggest.setPic(user.getPic());//用户手机号
            appSuggest.setNierong(neirong);//内容
            appSuggest.setTupian(images);
            appSuggest.setCreatetime(new Date());
            appSuggestService.saveOrUpdate(appSuggest);
            return this.sucessJSON("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }


    /**
     * @Description 联系我们
     * @Param: []
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/10/23
     */
    @ResponseBody
    @RequestMapping("/lianxiwomen")
    public Result lianxiwomen() {
        try {
            AppSetup appSetup = appSetupService.get();
            Map<String, Object> tomap = new HashMap<>(16);
            tomap.put("lianxi", appSetup.getLianxi());
            return this.sucessJSON(tomap, "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }


    /**
     * 帮助列表
     *
     * @param type 1常见问题 2账号问题 3钻石问题 4认证问题 5金币问题 6热门问题 7其他
     * @return
     */
    @ResponseBody
    @RequestMapping("/helpList")
    public Result helpList(@RequestParam(value = "type", required = false) Integer type,
                           @RequestParam(value = "title", required = false) String title) {
        try {
            List<AppHelp> appHelpList = appHelpService.findList(type, title);
            return this.sucessJSON(appHelpList);
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

}
